/**
 * //Describe how you could use a single array to implement three stacks.
 * //
 * // You should implement push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、
 * //peek(stackNum) methods. stackNum is the index of the stack. value is the value
 * //that pushed to the stack.
 * //
 * // The constructor requires a stackSize parameter, which represents the size of
 * //each stack.
 * //
 * // Example1:
 * //
 * //
 * // Input:
 * //["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
 * //[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
 * // Output:
 * //[null, null, null, 1, -1, -1, true]
 * //Explanation: When the stack is empty, `pop, peek` return -1. When the stack
 * //is full, `push` does nothing.
 * //
 * //
 * // Example2:
 * //
 * //
 * // Input:
 * //["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
 * //[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
 * // Output:
 * //[null, null, null, null, 2, 1, -1, -1]
 * //
 * //
 * // Related Topics 栈 设计 数组 👍 55 👎 0
 */

package com.xixi.dataStructure.stack;

public class ID_interview_03_01_ThreeInOneLcci {
    public static void main(String[] args) {
        TripleInOne solution = new ID_interview_03_01_ThreeInOneLcci().new TripleInOne(3);
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class TripleInOne {

        private int[] storeArray;
        private int stackTotalSize = 0;


        private int stackOneTop = -3; //pin to topValue; init as minus value for empty stack
        private int stackOneSize = 0;
        private int stackTwoTop = -2; //pin to topValue;
        private int stackTwoSize = 0;
        private int stackThreeTop = -1; //pin to topValue;
        private int stackThreeSize = 0;

        public TripleInOne(int stackSize) {
            storeArray = new int[stackSize * 3];
            this.stackTotalSize = stackSize;
        }

        public void push(int stackNum, int value) {
            switch (stackNum) {
                case 0:
                    if (stackOneSize < stackTotalSize) {//checkFull
                        stackOneSize++;
                        stackOneTop += 3; //move pin
                        storeArray[stackOneTop] = value;//put in array
                    }
                    break;
                case 1:
                    if (stackTwoSize < stackTotalSize) {//checkFull
                        stackTwoSize++;
                        stackTwoTop += 3; //move pin
                        storeArray[stackTwoTop] = value;//put in array
                    }
                    break;
                case 2:
                    if (stackThreeSize < stackTotalSize) {//checkFull
                        stackThreeSize++;
                        stackThreeTop += 3; //move pin
                        storeArray[stackThreeTop] = value;//put in array
                    }
                    break;
            }


        }

        public int pop(int stackNum) {
            int res = -1;

            switch (stackNum) {
                case 0:
                    if (stackOneSize > 0) {//checkFull
                        stackOneSize--;
                        res = storeArray[stackOneTop];
                        stackOneTop -= 3; //move pin
                    }
                    break;
                case 1:
                    if (stackTwoSize > 0) {//checkFull
                        stackTwoSize--;
                        res = storeArray[stackTwoTop];
                        stackTwoTop -= 3; //move pin
                    }
                    break;
                case 2:
                    if (stackThreeSize > 0) {//checkFull
                        stackThreeSize--;
                        res = storeArray[stackThreeTop];
                        stackThreeTop -= 3; //move pin
                    }
                    break;
            }

            return res;
        }

        public int peek(int stackNum) {

            int res = -1;

            switch (stackNum) {
                case 0:
                    if (stackOneSize > 0) {//checkFull
                        res = storeArray[stackOneTop];
                    }
                    break;
                case 1:
                    if (stackTwoSize > 0) {//checkFull
                        res = storeArray[stackTwoTop];
                    }
                    break;
                case 2:
                    if (stackThreeSize > 0) {//checkFull
                        res = storeArray[stackThreeTop];
                    }
                    break;
            }

            return res;


        }

        public boolean isEmpty(int stackNum) {


            boolean res = false;

            switch (stackNum) {
                case 0:
                    if (stackOneSize == 0) {//checkFull
                        res = true;
                    }
                    break;
                case 1:
                    if (stackTwoSize == 0) {//checkFull
                        res = true;
                    }
                    break;
                case 2:
                    if (stackThreeSize == 0) {//checkFull
                        res = true;
                    }
                    break;
            }

            return res;

        }
    }

/**
 * Your TripleInOne object will be instantiated and called as such:
 * TripleInOne obj = new TripleInOne(stackSize);
 * obj.push(stackNum,value);
 * int param_2 = obj.pop(stackNum);
 * int param_3 = obj.peek(stackNum);
 * boolean param_4 = obj.isEmpty(stackNum);
 */
//leetcode submit region end(Prohibit modification and deletion)


}